package com.pri.chat.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.TxBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.model.TxModel;
import com.pri.chat.model.TxMsgModel;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.StringUtils;
import com.vondear.rxtool.view.RxToast;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/4/29
 */
public class AddTxActivity extends BaseActivity {
    @BindView(R.id.et_true_name)
    EditText etTrueName;
    @BindView(R.id.et_ali_name)
    EditText etAliName;
    @BindView(R.id.et_login_name)
    EditText etLoginName;
    @BindView(R.id.changeAccount)
    TextView changeAccount;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_tx_layout;
    }

    @Override
    protected void initView() {
        setTitle("账户信息绑定");
        btnRight.setText("保存");
        btnRight.setVisibility(View.VISIBLE);
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isNull(etTrueName)){
                    RxToast.normal("请输入真实姓名");
                    return;
                }
                if(isNull(etAliName)&&isNull(etLoginName)){
                    RxToast.normal("请至少输入一个提现信息");
                    return;
                }
                tixian();
            }
        });

        changeAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxToast.normal("暂不可更换，请联系客服");
            }
        });

    }
    private void tixian() {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            RxToast.normal("保存成功");
            SharedHelper.saveKeyValueForString(mContext,"trueName",getString(etTrueName));
            SharedHelper.saveKeyValueForString(mContext,"zfbNo",getString(etAliName));
            SharedHelper.saveKeyValueForString(mContext,"wxNo", getString(etLoginName));
            setResult(3);
            finish();
        };
        TxMsgModel regModel = new TxMsgModel();//登录上传参数
        regModel.setId(SharedHelper.readId(this));
        regModel.setTrueName(getString(etTrueName));
        regModel.setWxNo(getString(etLoginName));
        regModel.setZfbNo(getString(etAliName));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(regModel));
        HttpMethods.getInstance().updateTiXianInfo(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }
    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<TxBean>> onNextListener = baseBean -> {
            etTrueName.setText(baseBean.getData().getTrueName());
            etAliName.setText(baseBean.getData().getZfbNo());
            etLoginName.setText(baseBean.getData().getWxNo());
            String ty = "";
            if(!StringUtils.isEmpty(etAliName.getText().toString())){
                etAliName.setFocusable(false);
                etAliName.setFocusableInTouchMode(false);
                ty += "1";
                changeAccount.setVisibility(View.VISIBLE);
            }
            if(!StringUtils.isEmpty(etTrueName.getText().toString())){
                etTrueName.setFocusable(false);
                etTrueName.setFocusableInTouchMode(false);
                ty += "1";
            }
            if(!StringUtils.isEmpty(etLoginName.getText().toString())){
                etLoginName.setFocusable(false);
                etLoginName.setFocusableInTouchMode(false);
                ty += "1";
            }
            if(ty.equals("111")){
                btnRight.setVisibility(View.GONE);
            }
        };
        TxModel regModel = new TxModel();//登录上传参数
        regModel.setMemberId(SharedHelper.readId(this));
        regModel.setType(2);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(regModel));
        HttpMethods.getInstance().getInfoByMemberId(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }


}
